package com.tang.algorithm.array;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author tlm
 * @version 1.0
 * @date 2020/5/30 11:37 上午
 */
public class LeetCode1002 {
    public static void main(String[] args) {
        String[] A = {"cool", "lock", "cook"};
        commonChars(A).forEach(s -> System.out.println(s));
    }

    public static List<String> commonChars(String[] A) {
        List<String> res = new ArrayList<>();
        int[] ans = new int[26];
        for (int i = 0; i < 26; i++) {
            ans[i] = Integer.MAX_VALUE;
        }
        for (String s : A) {
            int[] temp = new int[26];
            for (int i = 0; i < 26; i++) {
                temp[i] = 0;
            }
            //统计字符出现的次数
            for (char c : s.toCharArray()) {
                temp[c - 'a']++;
            }
            for (int i = 0; i < 26; i++) {
                ans[i] = Math.min(ans[i], temp[i]);
            }
        }

        for (int i = 0; i < 26; i++) {
            while (ans[i] > 0) {
                res.add(String.valueOf((char) ('a' + i)));
                ans[i]--;
            }

        }
        return res;
    }
}
